﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BLL.IntelligenceSearch
{
    class SearchShop:ASearch
    {
        private String shopViewName = "V_SHOPBRIEF";
        private List<String> shopSearchWord = new List<string>();
        private int maxReturnCount = 10;
        public SearchShop(List<String> searchword, int maxReturnCount)
        {
            this.maxReturnCount = maxReturnCount;
            this.shopSearchWord = searchword;
            InitFieldWeight();
        }
        /// <summary>
        /// 数据库中匹配到的字段的权重设置
        /// </summary>
        private void InitFieldWeight()
        {
            //如果需要配置每个字段匹配的权重，可以修改这个函数
            fieldWeight.Add("nickname", 2);
            fieldWeight.Add("fullname", 3);
            fieldWeight.Add("keyword1", 1);
            fieldWeight.Add("keyword2", 1);
            fieldWeight.Add("keyword3", 1);
        }

        public override ArrayList DoSearch()
        {
            if (shopSearchWord.Count == 1 && shopSearchWord[0].Length > 3
                && ToolBLL.isNumberic(shopSearchWord[0]))
            {
                //当分词结果只有一个数字字符串，并且字符串长度>3时
                //猜测可能是多巴拉码
                //算法从多巴拉码中进行搜索，而不从公司名，昵称，关键词中搜索
                //TODO 重构这部分算法
                SearchDB(shopViewName, "dblno", shopSearchWord[0], "1");
            }
            foreach (String s in shopSearchWord)
            {

                foreach (DictionaryEntry entry in fieldWeight)
                {
                    SearchDB(shopViewName, entry.Key.ToString(), s,"1");
                }
            }

            int listCount = resultList.Count;
            if (listCount >= maxReturnCount)
            {
                return SortResultList(resultList).GetRange(0, maxReturnCount);
            }
            else
            {
                return SortResultList(resultList).GetRange(0, listCount);
            }
        }

        public override ArrayList DoSearchAll()
        {
            if (shopSearchWord.Count == 1 && shopSearchWord[0].Length > 3
                && ToolBLL.isNumberic(shopSearchWord[0]))
            {
                //当分词结果只有一个数字字符串，并且字符串长度>3时
                //猜测可能是多巴拉码
                //算法从多巴拉码中进行搜索，而不从公司名，昵称，关键词中搜索
                //TODO 重构这部分算法
                SearchDB(shopViewName, "dblno", shopSearchWord[0], "");
            }
            foreach (String s in shopSearchWord)
            {

                foreach (DictionaryEntry entry in fieldWeight)
                {
                    SearchDB(shopViewName, entry.Key.ToString(), s, "");
                }
            }

            int listCount = resultList.Count;
            if (listCount >= maxReturnCount)
            {
                return SortResultList(resultList).GetRange(0, maxReturnCount);
            }
            else
            {
                return SortResultList(resultList).GetRange(0, listCount);
            }
        }
    }
}
